﻿using Abp.Dependency;
using Microsoft.AspNetCore.Identity;
using System;

namespace Batman.Jianbei.Forum.Core.Domains
{
    public class User : IdentityUser
    {
        private DateTime? _createAt = null;

        public DateTime CreateAt
        {
            get
            {
                return this._createAt.HasValue ? this._createAt.Value : DateTime.Now;
            }
            set
            {
                this._createAt = value;
            }
        }

        /// <summary>
        /// 设置密码，存入对象的PasswordHash属性
        /// </summary>
        /// <param name="password">明文密码</param>
        public void SetPassword(string password)
        {
            var userManager = IocManager.Instance.Resolve<UserManager<User>>();
            var passwordHashed = userManager.PasswordHasher.HashPassword(this, password);

            this.PasswordHash = passwordHashed;
        }

        /// <summary>
        /// 用户的密码是不是等于参数传入的明文密码
        /// </summary>
        /// <param name="password">明文密码</param>
        /// <returns></returns>
        public bool PasswordIs(string password)
        {
            var userManager = IocManager.Instance.Resolve<UserManager<User>>();
            var passwordHashed = userManager.PasswordHasher.HashPassword(this, password);

            return this.PasswordHash == passwordHashed;
        }
    }
}
